<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.1/jquery.js"></script>
    <style>
        .img-show {
            display: flex;
        }
        .img-box{
            position: relative;
            width: 100px;
            height: 100px;
        }
        img {
            display: block;
            width: 100%;
            height: 100%;
        }

        .all {
            position: relative;
            margin: 10px 0;
        }

        #picker {
            width: 100px;
            height: 30px;
            box-sizing: border-box;
            border: 1px solid #ddd;
            border-radius: 4px;
            text-align: center;
            line-height: 30px;
            background-color: #fff;
            font-size: 14px;
            color: #333;
            transition: 0.2s;
        }

        #picker:hover {
            border-color: #00aeec;
        }

        .file {
            width: 1px;
            height: 1px;
            position: absolute;
            z-index: -100;
            left: 0;
            top: 0;
            opacity: 0;
        }

        #ctlBtn {
            cursor: pointer;
            width: 80px;
            height: 30px;
            text-align: center;
            font-size: 14px;
            background-color: #00aeec;
            border: 1px solid #ddd;
            border-radius: 4px;
            line-height: 30px;
            color: #fff;
            transition: 0.2s;
        }

        #ctlBtn:hover {
            background-color: #008ac5;
        }

        .img-show {
            margin-top: 10px;
        }
        .message{
            margin: 10px 0;
        }
        @keyframes itemIn {
            from{
                top:0
            }to{
                top:100px
            }
        }
    </style>
</head>

<body>
    <div id="uploader" class="wu-example">
        <label>
            <div class="all">
                <div id="picker">选择文件</div>
                <input class="file" type="file">
            </div>
        </label>
        <div id="ctlBtn" class="btn btn-default">开始上传</div>
        <div class="message">未选择图片</div>
    </div>
    <div class="img-show">
        <div class="img-box" style="width: 100px;height: 100px;">
            <img style="display: none;" class="img" src="#" alt="">
        </div>
    </div>
    <script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
    <script>
        jQuery(function () {
            let param = null,
                file = null,
                intercept = false;
            jQuery(".file").on({
                change: function (e) {
                    if ((e.target.files[0].type).indexOf("image/") == -1) {
                        file = null;
                        jQuery(".message").text('请选择图片文件');
                        return;
                    }
                    file = e.target.files[0];
                    param = new FormData();
                    param.append('cover', e.target.files[0], e.target.files[0].name);
                    jQuery(".img").attr("src", window.URL.createObjectURL(e.target.files[0])).css({
                        display: "block"
                    });
                    jQuery(`<div class="mask"></div>`).css({
                        width:"100%",
                        height:"100%",
                        position:"absolute",
                        left:0,
                        top:0,
                        animation: "itemIn 0.3s linear forwards",
                        backgroundColor:"#fff",
                        zIndex:1000
                    }).appendTo(jQuery(".img-box"));
                    jQuery(".message").css({
                        color:"#333"
                    }).text('待上传');
                    setTimeout(()=>{
                        jQuery(".img-box .mask").remove();
                    },300);
                }
            })
            jQuery("#ctlBtn").on({
                click: async function () {
                    if(intercept)return;
                    intercept = true;
                    if (!file){
                        jQuery(".message").text("请选择图片").css({
                            color:"red"
                        });
                        intercept = false;
                        return;
                    }
                    let dot = "",
                        time = setInterval(()=>{
                        if(dot.length>=3)dot="";
                        dot += ".";
                        jQuery(".message").css({
                            color:"#333"
                        }).text(`正在上传${dot}`);
                    },500),
                        { data: { token } } = await jQuery.ajax({
                        type: "post",
                        url: "https://yw.52kfw.cn/api/user/phone-login",
                        data: {
                            phone: "18620628729",
                            code: "1234"
                        }
                    }),
                        result = await jQuery.ajax({
                        type: "post",
                        url: "https://yw.52kfw.cn/api/upload/store",
                        headers: {
                            "Authorization": "Bearer " + token
                        },
                        contentType: false,
                        processData: false,
                        data: param,
                    });
                    clearInterval(time);
                    if(result.statusCode==200){
                        jQuery(".message").text(`上传成功`);
                    }else{
                        jQuery(".message").text(`服务器繁忙`);
                    }
                    console.log(token, result);
                    localStorage.token = token;
                    setTimeout(()=>{
                        intercept = false;
                    },500)
                }
            })
        });
    </script>
</body>

</html>